import axios from "axios"
// vue使用进度条
import NProgress from "nprogress"
import 'nprogress/nprogress.css'
// const BASE_API_URL="http://81.68.221.202:8081/"
const BASE_API_URL="http://localhost:8081/"
const service = axios.create({
    baseURL:BASE_API_URL,
    timeout:5000
})


// // 请求拦截器
service.interceptors.request.use(config=>{

    // 请求进度条开始
    NProgress.start()
    // 在请求头Authorization中携带jwt token
    let pathname=location.pathname
    console.log(pathname);
    config.headers.Authorization=localStorage.getItem("token")
    return config
  }
  , error=>{
    return Promise.reject(error)
  }
  )
  
service.interceptors.response.use(
    function(response){
      NProgress.done()
      const res=response.data
      if(res.success){
        return response.data
      }
      if (res.data!=null&&res.errorCode === 50001) {
        localStorage.clear()
        return router.push("/login");
    }
    return response.data;
    },
    function (error) {
        return Promise.reject(error)
    }
  )
  

export default service